/**
 * Created by Mark_ren on 2017/5/31.
 * 苟利国家生死以，岂因祸福避趋之
 */
var item = getQueryString("item");
var sort = getQueryString("sort");
$(function () {
    var title = "";
    var sel = $.Params.create();
    sel.addParams("page",0);
    sel.addParams("rows",rows);
    sel.addParams("item",item);
    sel.addParams("sort",sort);
    getData();

    //查询
    $("#searchBtn").on("click",function(){
        var searchVal = $("#searchVal").val();
        sel.addParams("title",searchVal);
        getData();
    })

    //判断checkbox状态
    $("#selAll").on("click",function () {
        if(!$(this).attr("checked")){
            $("input[type=checkbox]").attr("checked","checked")
        }else{
            $("input[type=checkbox]").removeAttr("checked");
        }
    })

    //添加事件
    $('#addDataBtn').on("click",function(){
        window.location.href="czypdAdd.html?item="+item+"&sort="+sort;
    });

    //删除事件
    $('#delBtn').on("click",function(){
        var pararms = getCheckData();
        delData(pararms);
    });

    //获取数据
    function getData(){
        getPageData(sel,getIP()+"api/v2.0/admin/articles/JW/",function(data){
            if(data.success){
                var html = "";
                $.each(data.result, function(i,val) {
                        html+='<tr role="row" class="odd">' +
                            "<td><input eid='"+val.id+"' onclick='getCheck(this)' type='checkbox' class='classOption'></td>" +
                            "<td><img style='height:100px;width:100px;' src="+getIP()+"data/articleCover/"+val.titlePic+"></td>" +
                            '<td>'+val.title+'</td>' +
                            '<td>'+val.createTime+'</td>' +
                            "<td><button  uid='"+val.id+"' onclick='updateInfo(this)'type='button' class='btn btn-primary'>修改</button><button type='button' onClick = 'setTop(\"" + val.id + "\")' class='btn btn-primary'>置顶</button></td></tr>";
                });
                $("#listContainer").html(html);
            }
        })
    }
    //置顶
        setTop = function(eid){
            var topSel = $.Params.create();
            topSel.addParams("id",eid);
            topSel.ajax(getAdminIP()+"articles/TOP/",function(data){
                if(data.success){
                    getData();
                }else{
                    alert(data.msg);
                }
            })
        }
    //删除数据
    function delData(ids){
        if(!ids == ""){
            var del = $.Params.create();
            del.addParams("params",ids)
            del.ajax(getIP()+"api/v2.0/admin/articles/del/",function(data){
                if(data.success){
                    alert("删除成功！");
                    getData();
                }
            })
        }else{
            alert("请至少选择一项");
        }
    }

    //修改数据
    updateInfo = function (obj) {
        var uId = $(obj).attr("uid");
        window.location.href="czypdUpdate.html?uid="+uId+"&item="+item+"&sort="+sort;
    }
});
